"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const global_1 = require("../../../config/global");
function registerStartNode(lf) {
    lf.register('startNode', ({ RectNode, RectNodeModel, h }) => {
        class Node extends RectNode {
            getTitleElement() {
                const { model } = this.props;
                const { x, y } = model;
                // 创建一个文本输入框
                return h("g", {}, [
                    h('text', {
                        fill: '#000000',
                        fontSize: 16,
                        x: x - 50,
                        y: y - 30,
                        width: 50,
                        height: 25
                    }, 'CC_StartNode')
                ]);
            }
            ;
            getLabelShape() {
                const { model } = this.props;
                const { x, y } = model;
                const nodeData = model.getData();
                const style = model.getNodeStyle();
                const properties = model.getProperties();
                return h('g', {}, [
                    h('text', {
                        fill: '#000000',
                        fontSize: 12,
                        x: x - 100,
                        y: y,
                        width: 50,
                        height: 25
                    }, 'Name:'),
                    h('text', {
                        fill: '#000000',
                        fontSize: 12,
                        x: x - 100,
                        y: y + 30,
                        width: 50,
                        height: 25
                    }, 'ParentID:'),
                    h("foreignObject", Object.assign(Object.assign({}, style), { id: "startName", type: "startName", height: 22, width: 150, y: y - 15, x: x - 45 }), [
                        h("textarea", {
                            style: {
                                width: "95%",
                                height: "70%",
                                resize: "none" // 禁止调整文本框大小
                            },
                            value: properties.startName,
                            onInput: (event) => {
                                let newContent = event.target.value;
                                properties.startName = newContent;
                                model.setProperties(properties);
                            },
                        })
                    ]),
                    h("foreignObject", Object.assign(Object.assign({}, style), { id: "startID", type: "startID", height: 22, width: 150, y: y + 15, x: x - 45 }), [
                        h("textarea", {
                            style: {
                                width: "95%",
                                height: "70%",
                                resize: "none" // 禁止调整文本框大小
                            },
                            value: properties.startID,
                            onInput: (event) => {
                                let newContent = event.target.value;
                                properties.startID = newContent;
                                model.setProperties(properties);
                            },
                        })
                    ]),
                ]);
            }
            getShape() {
                const { model } = this.props;
                const { width, height, x, y, } = model;
                const style = model.getNodeStyle();
                const properties = model.getProperties();
                return h("g", {}, [
                    h("rect", Object.assign(Object.assign({}, style), { id: "svg_1", height: 75, width: 220, x: x - 220 / 2, y: y - 25, fill: "#fe9456" })),
                    h("rect", Object.assign(Object.assign({}, style), { id: "svg_7", height: 30, width: 220, x: x - 220 / 2, y: y - 50 })),
                    this.getLabelShape(),
                    this.getTitleElement(),
                ]);
            }
        }
        class Model extends RectNodeModel {
            constructor(data, graphModel) {
                data.text = {
                    value: (data.text && data.text.value) || '未命名节点-开始',
                    x: data.x,
                    y: data.y + 70
                };
                super(data, graphModel);
                this.radius = 20;
            }
            initNodeData(data) {
                if (!data.virtual && !data.properties) {
                    const start = new global_1.StartNode();
                    data.properties = Object.assign({}, start);
                    this.width = 220;
                    this.height = 100;
                }
                super.initNodeData(data);
                const circleOnlyAsTarget = {
                    message: "开始节点的下一个节点不能是奖励",
                    validate: (sourceNode, targetNode, sourceAnchor, targetAnchor) => {
                        return targetNode.type !== "rewardNode";
                    },
                };
                const onlyOneAsConnect = {
                    message: "只能连接一个节点",
                    validate: (sourceNode, targetNode, sourceAnchor, targetAnchor) => {
                        return sourceNode.outgoing.nodes.length <= 0;
                    },
                };
                const onlyPosAsConnect = {
                    message: "连接对应的锚点",
                    validate: (sourceNode, targetNode, sourceAnchor, targetAnchor) => {
                        return sourceAnchor.type === 'right' && targetAnchor.type === 'left';
                    },
                };
                this.sourceRules.push(onlyPosAsConnect);
                this.sourceRules.push(circleOnlyAsTarget);
                this.sourceRules.push(onlyOneAsConnect);
            }
            addField() {
                this.setAttributes();
                // 更新节点连接边的path
                this.incoming.edges.forEach((egde) => {
                    // 调用自定义的更新方案
                    egde.updatePathByAnchor();
                });
                this.outgoing.edges.forEach((edge) => {
                    // 调用自定义的更新方案
                    edge.updatePathByAnchor();
                });
            }
            getConnectedTargetRules() {
                const rules = super.getConnectedTargetRules();
                const notAsTarget = {
                    message: "起始节点不能作为边的终点",
                    validate: () => false,
                };
                rules.push(notAsTarget);
                return rules;
            }
            getAnchorStyle(anchorInfo) {
                const style = super.getAnchorStyle(anchorInfo);
                if (anchorInfo.type === "left") {
                    style.fill = "red";
                    style.hover.fill = "transparent";
                    style.hover.stroke = "transpanrent";
                    style.className = "lf-hide-default";
                }
                else {
                    style.fill = "green";
                }
                return style;
            }
            setIsShowAnchor() {
                this.isShowAnchor = true;
            }
            getDefaultAnchor() {
                const { width, height, x, y, id } = this;
                return [{
                        x: x + 16 - width / 2,
                        y: y + 16 - height / 2,
                        type: "left",
                        edgeAddable: false,
                        id: `${id}_0`,
                    },
                    {
                        x: x - 16 + width / 2,
                        y: y + 16 - height / 2,
                        type: "right",
                        id: `${id}_1`,
                    },
                ];
            }
        }
        return {
            view: Node,
            model: Model
        };
    });
}
exports.default = registerStartNode;
